﻿-- -----------------------------------------------------------------------------
-- | WonderTek [ 网络无处不在，沟通及时到达 ]
-- -----------------------------------------------------------------------------
-- | Copyright (c) 2012, WonderTek, Inc. All Rights Reserved.
-- -----------------------------------------------------------------------------
-- | Author: zhuqian <zhuqian@mantis.com>
-- -----------------------------------------------------------------------------
-- @class WebBrowser
-- @note  浏览器相关操作
-- -----------------------------------------------------------------------------

WebBrowser = {}

Reg.g_webBrowser = 'g_webBrowser'

--[[
  ------------------------------------------------------------------------------
  --@function WebBrowser:create(nLeft, nTop, nWidth, nHeight)
  ------------------------------------------------------------------------------
  --@brief 创建浏览器窗口
  ------------------------------------------------------------------------------
  --@access public
  ------------------------------------------------------------------------------
  --@param nLeft: number, 浏览器窗口左上角X坐标
  --@param nTop: number, 浏览器窗口左上角Y坐标
  --@param nWidth: number, 浏览器窗口宽度(像素)
  --@param nHeight: number, 浏览器窗口高度(像素)
  ------------------------------------------------------------------------------
  --@return number: 1为成功，0为失败
  ------------------------------------------------------------------------------
  --]]
function WebBrowser:create(nLeft, nTop, nWidth, nHeight)
    return pluginInvoke(self:_getHandle(), 'Create', nLeft, nTop, nWidth, nHeight)
end

--[[
  ------------------------------------------------------------------------------
  --@function WebBrowser:release()
  ------------------------------------------------------------------------------
  --@brief 关闭浏览器窗口
  ------------------------------------------------------------------------------
  --@access public
  ------------------------------------------------------------------------------
  --@param 无
  ------------------------------------------------------------------------------
  --@return nil
  ------------------------------------------------------------------------------
  --]]
function WebBrowser:release()
    pluginInvoke(self:_getHandle(), 'Release')
end

--[[
  ------------------------------------------------------------------------------
  --@function WebBrowser:openUrl(strUrl)
  ------------------------------------------------------------------------------
  --@brief 打开网址
  ------------------------------------------------------------------------------
  --@access public
  ------------------------------------------------------------------------------
  --@param strUrl: string, 网址
  ------------------------------------------------------------------------------
  --@return number: 1为成功，0为失败
  ------------------------------------------------------------------------------
  --]]
function WebBrowser:openUrl(strUrl)
    return pluginInvoke(self:_getHandle(), 'OpenUrl', strUrl)
end

--[[
  -----------------------------------------------------------------------------
  --@function WebBrowser:back()
  -----------------------------------------------------------------------------
  --@brief 网络后退功能
  ------------------------------------------------------------------------------
  --@access public
  -----------------------------------------------------------------------------
  --@param 无
  -----------------------------------------------------------------------------
  --@return nil
  -----------------------------------------------------------------------------
  --]]
function WebBrowser:back()
    return pluginInvoke(self:_getHandle(), 'HandleCommandL', 5)
end

--[[
  -----------------------------------------------------------------------------
  --@function WebBrowser:refresh()
  -----------------------------------------------------------------------------
  --@brief 网络刷新功能
  ------------------------------------------------------------------------------
  --@access public
  -----------------------------------------------------------------------------
  --@param 无
  -----------------------------------------------------------------------------
  --@return nil
  -----------------------------------------------------------------------------
  --]]
function WebBrowser:refresh()
    return pluginInvoke(self:_getHandle(), 'HandleCommandL', 4)
end

--[[
  -----------------------------------------------------------------------------
  --@function WebBrowser:advance()
  -----------------------------------------------------------------------------
  --@brief 网络前进功能
  ------------------------------------------------------------------------------
  --@access public
  -----------------------------------------------------------------------------
  --@param 无
  -------------------------------------------------------------------------------
  --@return nil
  -----------------------------------------------------------------------------
  --]]
function WebBrowser:advance()
    return pluginInvoke(self:_getHandle(), 'HandleCommandL', 6)
end

--[[
  -----------------------------------------------------------------------------
  --@function WebBrowser:setUrlResolve(playUrlType, downLoadUrlType, playXmlFunction, downloadXmlFunction, downloadStatusXmlFunction)
  -----------------------------------------------------------------------------
  --@brief 解析Url信息
  ------------------------------------------------------------------------------
  --@access public
  -----------------------------------------------------------------------------
  --@param playUrlType: string, 视频地址url的前缀
  --@param downLoadUrlType: string, 下载地址url的前缀
  --@param playXmlFunction: function, 视频地址所要运行的业务函数名字
  --@param downloadXmlFunction: function, 下载地址所要运行的业务函数名字
  --@param downloadStatusXmlFunction: function, 网页打开状态位需要运行的业务函数的名字
  -----------------------------------------------------------------------------
  --@return number: 1为成功，0为失败
  -----------------------------------------------------------------------------
  --]]
function WebBrowser:setUrlResolve(playUrlType, downLoadUrlType, playXmlFunction, downloadXmlFunction, downloadStatusXmlFunction)
    return pluginInvoke(self:_getHandle(), 'SetUrlResolve', playUrlType, downLoadUrlType, playXmlFunction, downloadXmlFunction, downloadStatusXmlFunction)
end

--[[
  ------------------------------------------------------------------------------
  --@function WebBrowser:setDownloadPath(downloadPath)
  ------------------------------------------------------------------------------
  --@brief 设置下载路径
  ------------------------------------------------------------------------------
  --@access public
  ------------------------------------------------------------------------------
  --@param downloadPath: string, 下载路径
  ------------------------------------------------------------------------------
  --@return number: 1为成功，0为失败
  ------------------------------------------------------------------------------
  --]]
function WebBrowser:setDownloadPath(downloadPath)
    return pluginInvoke(self:_getHandle(), 'SetDownloadPath', downloadPath)
end

--[[
  ------------------------------------------------------------------------------
  --@function WebBrowser:showWebBrowser(wParam)
  ------------------------------------------------------------------------------
  --@brief 是否显示浏览器窗口
  ------------------------------------------------------------------------------
  --@access public
  ------------------------------------------------------------------------------
  --@param wParam: number, 是否显示浏览器窗口(1为显示，0为隐藏)
  ------------------------------------------------------------------------------
  --@return number: 1为成功，0为失败
  ------------------------------------------------------------------------------
  --]]
function WebBrowser:showWebBrowser(wParam)
    return pluginInvoke(self:_getHandle(), 'ShowWebBrowser', wParam)
end

--[[
  ------------------------------------------------------------------------------
  --@function WebBrowser:isBrowserRun()
  ------------------------------------------------------------------------------
  --@brief 判断浏览器是否在运行
  ------------------------------------------------------------------------------
  --@access public
  ------------------------------------------------------------------------------
  --@param 无
  ------------------------------------------------------------------------------
  --@return number: 1为运行中，0为已关闭
  ------------------------------------------------------------------------------
--]]
function WebBrowser:isBrowserRun()
    return pluginInvoke(self:_getHandle(), 'IsBrowserRun')
end

--[[
  ------------------------------------------------------------------------------
  --@function WebBrowser:setCookie(url, cookies)
  ------------------------------------------------------------------------------
  --@brief 设置Cookie
  ------------------------------------------------------------------------------
  --@access public
  ------------------------------------------------------------------------------
  --@params url: string, url地址
  --@params cookies: string, 与url对应的cookies串
  ------------------------------------------------------------------------------
  --@return nil
  ------------------------------------------------------------------------------
--]]
function WebBrowser:setCookie(url, cookies)
    pluginInvoke(self:_getHandle(), "SetCookie", url, cookies)
end

--[[
  ------------------------------------------------------------------------------
  --@function WebBrowser:removeCookie()
  ------------------------------------------------------------------------------
  --@brief 清除Cookie
  ------------------------------------------------------------------------------
  --@access public
  ------------------------------------------------------------------------------
  --@param 无
  ------------------------------------------------------------------------------
  --@return nil
  ------------------------------------------------------------------------------
--]]
function WebBrowser:removeCookie()
    pluginInvoke(self:_getHandle(), "RemoveCookie")
end

--[[
  ------------------------------------------------------------------------------
  --@function WebBrowser:getCookie(url)
  ------------------------------------------------------------------------------
  --@brief 获取Cookie
  ------------------------------------------------------------------------------
  --@access public
  ------------------------------------------------------------------------------
  --@params url: string, url地址
  ------------------------------------------------------------------------------
  --@return string: 返回与url对应的cookies串
  ------------------------------------------------------------------------------
--]]
function WebBrowser:getCookie(url)
    return pluginInvoke(self:_getHandle(), "GetCookie", url)
end

--[[
 -------------------------------------------------------------------------------
 -- @function WebBrowser:_getHandle()
 -------------------------------------------------------------------------------
 -- @brief 获取浏览器插件句柄
 -------------------------------------------------------------------------------
 -- @access private
 -------------------------------------------------------------------------------
 -- @param 无
 -------------------------------------------------------------------------------
 -- @return number
 -------------------------------------------------------------------------------
 --]]
function WebBrowser:_getHandle()
    Log:write('WebBrowser:_getHandle()  Reg.g_webBrowser = ', Reg.g_webBrowser)
    local reg = registerCreate(Reg.g_webBrowser)
    Log:write('WebBrowser:_getHandle()  reg = ', reg)
    local webBrowserHandle = registerGetInteger(reg, 'webBrowserPlugin')
    Log:write('WebBrowser:_getHandle()  webBrowserHandle = ', webBrowserHandle)
    if webBrowserHandle == 0 then
        webBrowserHandle = pluginCreate('WebBrowser')
    Log:write('WebBrowser:_getHandle() webBrowserHandle == 0 webBrowserHandle = ', webBrowserHandle)
        registerSetInteger(reg, 'webBrowserPlugin', webBrowserHandle)
    Log:write('WebBrowser:_getHandle() webBrowserHandle == 0 webBrowserHandle = ', webBrowserHandle)
    end
    Log:write('WebBrowser:_getHandle() = ', webBrowserHandle)
    return webBrowserHandle
end

